import { legacy_createStore as createStore } from "redux";
// 1. action type
const ADD = "ADD";
const MINUS = "MINUS";
// 2. actionCreator
const add = () => ({ type: ADD });
const minus = () => ({ type: MINUS });
function reducer(state = { number: 0 }, action) {
  switch (action.type) {
    case ADD:
      return { number: state.number + 1 };
    case MINUS:
      return { number: state.number - 1 };
    default:
      return state;
  }
}
// 3. 创建 store
const store = createStore(reducer);
const valueEl = document.getElementById("value");
function render() {
  valueEl.innerHTML = store.getState().number;
}
render();
// 4. 回调
store.subscribe(render);

// 5. 事件
document
  .getElementById("add")
  .addEventListener("click", () => store.dispatch(add()));
document
  .getElementById("minus")
  .addEventListener("click", () => store.dispatch(minus()));
